package zsc.ruanc.practicaltraining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zsc.ruanc.practicaltraining.mapper.ReplyMapper;
import zsc.ruanc.practicaltraining.model.domain.TComment;
import zsc.ruanc.practicaltraining.model.domain.TReply;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.service.CommentService;
import zsc.ruanc.practicaltraining.service.RabbitMQService;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/comment")
@Validated
@Api(tags = "评论 + 回复 模块")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RabbitMQService rabbitMQService;


    @PostMapping("/insertComment")
    @ApiOperation(value = "插入一条评论", httpMethod = "POST")
    public JsonResult insertComment(@Validated @RequestBody TComment vo, HttpServletRequest httpServletRequest) {

        String ip = httpServletRequest.getRemoteAddr();
        String name = (String)httpServletRequest.getAttribute("name");
        int userId = (int) httpServletRequest.getAttribute("id");

        commentService.insertComment(vo, name, ip);

        rabbitMQService.commentMailbox(userId, name, vo.getArticleId()); // 通知作者
        return ResultUtils.success(true, "评论成功！！！");

    }

    @GetMapping("/deleteComment/{id}")
    @ApiOperation(value = "删除一条评论(管理员、普通用户公用)", httpMethod = "GET")
    public JsonResult deleteComment(@NotNull(message = "id不能为空") @Min(message = "参数不能小于1", value = 1) @PathVariable(value = "id",required = false) Integer id,
                                    HttpServletRequest httpServletRequest) {


        String name = (String)httpServletRequest.getAttribute("name");
        String role = (String) httpServletRequest.getAttribute("role");
        if (role.equals("Role_admin")) {
            commentService.deleteComment(id, null);
        } else {
            commentService.deleteComment(id, name);
        }
        return ResultUtils.success(true, "删除成功");
    }


//    @PostMapping("/updateComment")
//    @ApiOperation(value = "修改一条评论", httpMethod = "POST")
//    public JsonResult updateComment(@Validated @RequestBody TComment vo, HttpServletRequest httpServletRequest) {
//
//        String ip = httpServletRequest.getRemoteAddr();
//        int userId = (int)httpServletRequest.getAttribute("id");
//        commentService.updateComment(vo, userId, ip);
//
//        rabbitMQService.commentMailbox(userId, vo.getArticleId()); // 通知作者
//        return ResultUtils.success(true, "修改成功！！！");
//
//    }

    @GetMapping("/commentPage")
    @ApiOperation(value = "获得文章评论（分页）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cur", value = "当前页", dataType = "Integer", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "页大小", dataType = "Integer", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "articleId", value = "文章id", dataType = "Integer", paramType = "query")
    })
    public JsonResult commentList(@Min(message = "参数cur不能小于0", value = 0) @RequestParam(value = "cur", defaultValue = "1") Integer cur,
                                  @Min(message = "参数size不能小于0", value = 1) @RequestParam(value = "size", defaultValue = "10") Integer size,
                               @NotNull(message = "条件不能为空") @Min(message = "参数不能小于0", value = 0) @RequestParam(value = "articleId", required = false) Integer articleId,
                                  HttpServletRequest httpServletRequest) {

        return ResultUtils.success(commentService.selectPage(cur, size, articleId, false));
    }


    @GetMapping("/likesComment/{commentId}")
    @ApiOperation(value = "评论点赞|取消点赞", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论Id", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "flag", value = "true点赞|false取消点赞", dataType = "Boolean", paramType = "query")
    })
    public JsonResult likes(@Min(message = "参数不能小于1", value = 1) @NotNull(message = "评论id不能为空") @PathVariable(value = "commentId", required = false) Integer commentId,
                            @NotNull(message = "状态不能为空") @RequestParam(value = "flag", required = false) Boolean flag,
                            HttpServletRequest httpServletRequest) {

        int userId = (int)httpServletRequest.getAttribute("id");
        String name = (String) httpServletRequest.getAttribute("name");

        if (flag == true) {
            commentService.likesAndOneComment(commentId);
            rabbitMQService.likesCommentMailBox(userId, name, commentId); // 提醒评论作者被点赞
        } else {
            commentService.likesCutOneComment(commentId);
        }

        return ResultUtils.success("请求成功");
    }






//-------------回复功能------------------------------------------

    @PostMapping("/insertReply")
    @ApiOperation(value = "插入一条回复", httpMethod = "POST")
    public JsonResult insertReply(@Validated @RequestBody TReply vo, HttpServletRequest httpServletRequest) {

        String ip = httpServletRequest.getRemoteAddr();
        String fromName = (String) httpServletRequest.getAttribute("name");
        int id = (int) httpServletRequest.getAttribute("id");

        int key = commentService.insertReply(vo, fromName, ip);

        rabbitMQService.replyMailbox(id, fromName, key);

        return ResultUtils.success(true, "回复成功");

    }

    @GetMapping("/deleteReply/{replyId}")
    @ApiOperation(value = "删除一条回复(管理员、普通用户公用)", httpMethod = "GET")
    public JsonResult deleteReply(@Min(message = "参数不能小于1", value = 1) @NotNull(message = "id不能为空") @PathVariable("replyId") Integer replyId,
                                  HttpServletRequest httpServletRequest) {
        String name = (String) httpServletRequest.getAttribute("name");
        String role = (String) httpServletRequest.getAttribute("role");
        if (role.equals("Role_admin")) {
            commentService.deleteReply(replyId, null);
        } else {
            commentService.deleteReply(replyId, name);
        }

        return ResultUtils.success(true, "删除成功");
    }


    @GetMapping("/likesReply/{replyId}")
    @ApiOperation(value = "回复点赞|取消点赞", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "replyId", value = "回复id", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "flag", value = "true点赞|false取消点赞", dataType = "Boolean", paramType = "query")
    })
    public JsonResult likesReply(@Min(message = "参数不能小于1", value = 1) @NotNull(message = "回复id不能为空") @PathVariable(value = "replyId", required = false) Integer replyId,
                            @NotNull(message = "状态不能为空") @RequestParam(value = "flag", required = false) Boolean flag,
                            HttpServletRequest httpServletRequest) {

        int userId = (int)httpServletRequest.getAttribute("id");
        String name = (String) httpServletRequest.getAttribute("name");

        if (flag == true) {
            commentService.likesAndOneReply(replyId);
            rabbitMQService.likesReplyMailbox(userId, name, replyId);

        } else {
            commentService.likesCutOneReply(replyId);
        }

        return ResultUtils.success("请求成功");
    }





























    //--------------管理员权限----------------------------------------------

    @GetMapping("/commentPageAdmin")
    @ApiOperation(value = "获得文章评论（分页|管理员）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cur", value = "当前页", dataType = "Integer", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "页大小", dataType = "Integer", paramType = "query", defaultValue = "10")
    })
    public JsonResult commentList_admin(@Min(message = "参数cur不能小于0", value = 0) @RequestParam(value = "cur", defaultValue = "1") Integer cur,
                                        @Min(message = "参数size不能小于1", value = 1) @RequestParam(value = "size", defaultValue = "10") Integer size) {

        return ResultUtils.success(commentService.adminSelectPage(cur, size));
    }





}
